﻿using System.Buffers;
using System;
using System.Linq;
using System.Collections.Generic;
using Xc.DataAccess.Protocoals.WaterMeters.Extensions;
using Microsoft.Extensions.DependencyInjection;

namespace Xc.DataAccess.Protocoals.WaterMeters.Models
{
    /// <summary>
    /// 数据域
    /// </summary>
    public class DataArea
    {
        /// <summary>
        /// 数据域
        /// </summary>
        /// <param name="dataArea"></param>
        private DataArea()
        {

        }
        /// <summary>
        /// 从序列化转换为对象
        /// </summary>
        /// <param name="dataArea"></param>
        /// <returns></returns>
        public static DataArea FromSequence(ReadOnlySequence<byte> dataArea)
        {
            DataArea dataArea1 = new DataArea();
            dataArea1.Data = dataArea.Slice(0, dataArea.Length-2).ToArray();
            var d = dataArea.Slice(dataArea.Length - 2, 2);
            var reader = new SequenceReader<byte>(d);
            if (!reader.TryRead(2,out var data))
            {
                dataArea1.IsValidate = false;
                return dataArea1;
            }
            var a = data.ToArray();

            dataArea1.ReceiveDataCheck = BitConverter.ToUInt16(a);
            if (dataArea1.CurrentDataCheck == dataArea1.ReceiveDataCheck)
            {
                dataArea1.IsValidate = true;
            }
            else
            {
                dataArea1.IsValidate = false;

            }
            return dataArea1;
        }
        /// <summary>
        /// 将对象序列化
        /// </summary>
        /// <param name="dataArea"></param>
        /// <returns></returns>
        public static ReadOnlySequence<byte> ToSequence(DataArea dataArea)
        {
            var buffer = dataArea.Data.ToArray();
            List<byte> vs = new List<byte>();
            vs.AddRange(buffer);
            vs.AddRange(BitConverter.GetBytes(dataArea.CurrentDataCheck));
            return new ReadOnlySequence<byte>(vs.ToArray());
        }
        /// <summary>
        /// 校验结果
        /// </summary>
        public bool IsValidate { get; set; } = false;
        /// <summary>
        /// 数据域内容
        /// </summary>
        public byte[] Data { get; set; }
        /// <summary>
        /// 当前数据域校验值
        /// </summary>
        public ushort CurrentDataCheck 
        {
            get
            {
                if (Data.Length==0)
                {
                    return 0;
                }
                byte[] temp = new byte[2];
                for (int i = 0; i < this.Data.Length; i += 2)
                {
                    temp[0] = (byte)(this.Data[i] ^ temp[0]);
                    temp[1] = (byte)(this.Data[i + 1] ^ temp[1]);
                }
                temp[0] = (byte)(0x55 ^ temp[0]);
                temp[1] = (byte)(0xAA ^ temp[1]);
                var num = BitConverter.ToUInt16(temp.Reverse().ToArray());
                return num;
            }
        }
        /// <summary>
        /// 数据域校验和
        /// </summary>
        public UInt16 ReceiveDataCheck { get; set; }
    }
}
